package com.d0319;

public class AtmTest {
    static int money=1000;
     static Object ob = new Object();
     static Object ob2 = new Object();

    public void takeMoney(){
        while (true){
            synchronized (ob){

                if (money>0){
                    money=money-100;
                    System.out.println(Thread.currentThread().getName()+"  取走了100元，还剩"+money+"元");
                }else {
                    System.out.println("余额没了");
                    break;
                }try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void makeMoney(){
        while (true){
            synchronized (ob2){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                money=money+2000;
                System.out.println(Thread.currentThread().getName()+"  存了2000元，还剩"+money+"元");
            }
        }
    }

    public static void main(String[] args) {
        AtmTest atm = new AtmTest();
        Runnable re = ()->atm.takeMoney();
        Thread t1 = new Thread(re,"A");
        Thread t2 = new Thread(re,"B");
        Thread t3 = new Thread("C"){
            @Override
            public void run() {
                atm.makeMoney();
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }
}